﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace 第九次作业
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        ////定义校验邮箱正则
        //public bool IsEmail(string email)
        //{
        //    if (string.IsNullOrEmpty(email))
        //        return false;
        //    return Regex.IsMatch(email, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*");
        //}
        ////定义校验手机号正则
        //public static bool IsMobile(string mobile)
        //{
        //    if (string.IsNullOrEmpty(mobile))
        //        return false;
        //    return Regex.IsMatch(mobile, @"^(1)\d{10}$");
        //}
        //定义校验URL正则
        public static bool IsURL(string url)
        {
            if (string.IsNullOrEmpty(url))
                return false;
            return Regex.IsMatch(url, @"\b(\S+)://(\S+)\b");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string url = textBox1.Text;
                if (IsURL(url))
                {
                    WebClient client = new WebClient();
                    client.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:93.0) Gecko/20100101 Firefox/93.0");
                    byte[] pageData = client.DownloadData(url);
                    string pageHtml = Encoding.Default.GetString(pageData);

                    MatchCollection mc3 = Regex.Matches(pageHtml, @"\d{11}"); //满足pattern的匹配集合
                    foreach (Match match in mc3)
                    {
                        MessageBox.Show("手机号：" + match.ToString());
                    }

                    MatchCollection mc2 = Regex.Matches(pageHtml, @"\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*"); //满足pattern的匹配集合
                    foreach (Match match in mc2)
                    {
                        MessageBox.Show("邮箱："+ match.ToString());
                    }
                }
                else
                {
                    MessageBox.Show("URL格式输入错误");
                }
            }
            catch
            {
                MessageBox.Show("输入错误");
            }
        }
    }
}
